package com.salon.client.tabItems;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.salon.client.GreetingServiceAsync;
import com.salon.client.Salon;
import com.salon.client.beans.ClientBean;
import com.salon.client.beans.ParentPagingLoadResult;
import com.salon.client.beans.ProcedureBean;
import com.salon.client.widgets.ClientDialog;
import com.salon.client.widgets.ProcedureDialog;

public class ProcedureTabItemPanel extends ParentTabItemPanel<ProcedureBean>{

	public ProcedureTabItemPanel(String title) {
		super(title);
	}
	
	@Override
	protected void initComponents() {
		addButton("Добавить процедуру", "brit-add-user", new SelectionListener<ButtonEvent>() {
			
			public void componentSelected(ButtonEvent ce) {
				ProcedureDialog dlg = new ProcedureDialog(ProcedureTabItemPanel.this);
				dlg.show();
			}
			
		});
		
		/*addButton("Редактировать клиента", "brit-add-user", new SelectionListener<ButtonEvent>() {
			
			public void componentSelected(ButtonEvent ce) {
				if (selectedBean == null) {
					Info.display("Выберите клиента", "для редактирования");
					return;
				}
				ClientDialog dlg = new ClientDialog(ClientTabItemPanel.this, selectedBean);
				dlg.show();
			}
			
		});*/
		
		addButton("Удалить процедуру", "brit-delete-user", new SelectionListener<ButtonEvent>() {
			
			public void componentSelected(ButtonEvent ce) {
				if (selectedBean != null) {
					((GreetingServiceAsync) Registry.get(Salon.SERVICE))
					.deleteItem("procedures", selectedBean.getId(), 
							new AsyncCallback<Void>() {

								@Override
								public void onFailure(Throwable caught) {
									Info.display("Не удалось", "удалить процедура");
									
								}

								@Override
								public void onSuccess(Void result) {
									Info.display("Удаление процедуры", "успешно");
									ProcedureTabItemPanel.this.refreshStore();
									
								}
						
					});
				}
			}
		});
		super.initComponents();
	}
	
	@Override
	ColumnModel getColomnModel() {
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		
		ColumnConfig conf = new ColumnConfig();
		conf.setId("date");
		conf.setHeader("Дата");
		conf.setWidth(95);
		conf.setDateTimeFormat(DateTimeFormat.getFormat("dd.MM.yyyy HH:mm"));
		configs.add(conf);
		
		conf = new ColumnConfig("name", "Услуга", 100);
		configs.add(conf);
		
		conf = new ColumnConfig("masterName", "Мастер", 100);
		configs.add(conf);
		
		conf = new ColumnConfig("clientName", "Клиент", 100);
		configs.add(conf);
		
		conf = new ColumnConfig("cost", "Стоимость", 70);
		configs.add(conf);
		
		conf = new ColumnConfig("desc", "Описание", 100);
		configs.add(conf);

		return new ColumnModel(configs);
	}

	@Override
	RpcProxy<ParentPagingLoadResult<ProcedureBean>> getProxy() {
		RpcProxy<ParentPagingLoadResult<ProcedureBean>> proxy = new RpcProxy<ParentPagingLoadResult<ProcedureBean>>() {
			@Override
			protected void load(Object loadConfig,
					AsyncCallback<ParentPagingLoadResult<ProcedureBean>> callback) {
				PagingLoadConfig ploadConfig = (PagingLoadConfig) loadConfig;
				int offset = ploadConfig.getOffset();
				((GreetingServiceAsync) Registry.get(Salon.SERVICE))
						.loadProcedures(offset, ploadConfig.getLimit(),
								callback);

			}
		};
		
		return proxy;
	}
}
